<?php
/**
 * 附件管理
 */

class Content_AttachmentController extends Cms_AdminController {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init ();
	}
	
	public function indexAction() {
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		$this->view->setScriptFile ( 'cookie.js' );
		
		$this->view->setScriptFile ( 'admin/calendar/calendar.js' );
		$this->view->setScriptFile ( 'admin/calendar/lang/en.js' );
		
		$this->view->setCssFile ( 'calendar/jscal2.css' );
		$this->view->setCssFile ( 'calendar/border-radius.css' );
		$this->view->setCssFile ( 'calendar/win2k.css' );
		
		$filename = $this->_request->get ( 'filename' );
		$start_uploadtime = $this->_request->get ( 'start_uploadtime' );
		$end_uploadtime = $this->_request->get ( 'end_uploadtime' );
		$fileext = $this->_request->get ( 'fileext' );
		
		$DB_Attachment = new DB_Attachments ();
		$where = ' 1 = 1 ';
		
		if (! empty ( $filename )) {
			$where .= " and save_name like '%$filename%' ";
		}
		
		if (! empty ( $start_uploadtime )) {
			$where .= " and ctime>='$start_uploadtime 00:00:00' ";
		}
		
		if (! empty ( $end_uploadtime )) {
			$where .= " and ctime<='$end_uploadtime 23:59:59' ";
		}
		
		if (! empty ( $fileext )) {
			$where .= " and ext='$fileext' ";
		}
		
		$Page = new Cms_Pager ( $DB_Attachment );
		$Pagelimit = $Page->loadPagerData ( 10, $where );
		
		$Image_list = $DB_Attachment->loadList ( '*', $where, "attach_id DESC", $Pagelimit );
		
		if ($Image_list && is_array ( $Image_list )) {
			foreach ( $Image_list as &$val ) {
				if ($val ['attach_type'] == 'content') {
					$val ['attach_type_desc'] = '新闻图片';
				}
				$val ['filesize'] = sizecount ( $val ['filesize'] );
			}
		}
		
		$this->view->assign ( "pageBlock", $Page->loadPagerTpl () );
		$this->view->assign ( 'Image_list', $Image_list );
		
		$this->render ( 'Content/attachment/index', null, true );
	}
	
	/**
	 * 内容附件上传
	 */
	public function attachmentsAction() {
		$this->view->setScriptFile ( 'admin/ajaxfileupload.js' );
		
		$id = $this->_request->get ( 'id' );
		$this->view->assign ( 'id', $id );
		$this->render ( 'Content/attachment/attachments', null, true );
	}
	
	/**
	 * 文件上传
	 */
	public function uploadAction() {
		
		if (! isset ( $_FILES ["Filedata"] ) || ! is_uploaded_file ( $_FILES ["Filedata"] ["tmp_name"] ) || $_FILES ["Filedata"] ["error"] != 0) {
			$msg = array (
					'code' => 0,
					'msg' => '请选择要上传的文件!' 
			);
			echo json_encode ( $msg );
		} else {
			// 检查文件类型
			$file_contenttype = mime_contenttype ( $_FILES ['Filedata'] ['name'] );
			if (substr ( $file_contenttype ['mime'], 0, 5 ) != 'image') {
				$msg = array (
						'code' => - 1,
						'msg' => '文件类型错误!' 
				);
			}
			
			$uploadfile_model = new Model_UploadFile ( 1000, 'preview' );
			$_uploadfile_info = $uploadfile_info = $uploadfile_model->previewImg ( 'Filedata' );
			
			if (! $uploadfile_info && empty ( $uploadfile_info )) {
				$msg = array (
						'code' => - 2,
						'msg' => '文件上传错误,请重新上传图片!' 
				);
			} else {
				$config = Zend_Registry::get ( 'config' );
				$_uploadfile_info = str_replace ( UPLOAD_PATH, $config->view_conf->siteUploadPath, $_uploadfile_info );
				$uploadfile_info = str_replace ( DIRECTORY_SEPARATOR, '/', $_uploadfile_info );
			}
			
			// 插入图片
			if ($uploadfile_info) {
				$msg = array (
						'code' => 1,
						'msg' => '上传成功!',
						'img_src' => $uploadfile_info 
				);
			
			} else {
				$msg = array (
						'code' => - 2,
						'msg' => '文件上传错误,请重新上传图片!' 
				);
			}
			
			echo json_encode ( $msg );
			exit ();
		}
	}
	
	/**
	 * 图库
	 */
	public function albumloadAction() {
		$dosubmit = $this->_request->get ( 'dosubmit' );
		
		$this->view->setScriptFile ( 'admin/admin_common.js' );
		$this->view->setScriptFile ( 'cookie.js' );
		
		$this->view->setScriptFile ( 'admin/calendar/calendar.js' );
		$this->view->setScriptFile ( 'admin/calendar/lang/en.js' );
		
		$this->view->setCssFile ( 'calendar/jscal2.css' );
		$this->view->setCssFile ( 'calendar/border-radius.css' );
		$this->view->setCssFile ( 'calendar/win2k.css' );
		
		if (isset ( $dosubmit )) {
			$filename = $this->_request->get ( 'filename' );
			$uploadtime = $this->_request->get ( 'uploadtime' );
			
			$DB_Attachment = new DB_Attachments ();
			$where = ' 1 = 1 ';
			
			if (! empty ( $filename )) {
				$where .= " and save_name like '%$filename%' ";
			}
			
			if (! empty ( $uploadtime )) {
				$where .= " and ctime>='$uploadtime 00:00:00' and ctime <= '$uploadtime 23:59:59' ";
			}
			
			$Page = new Cms_Pager ( $DB_Attachment );
			$Pagelimit = $Page->loadPagerData ( 8, $where );
			
			$Image_list = $DB_Attachment->loadList ( '*', $where, "attach_id DESC", $Pagelimit );
			
			$this->view->assign ( "pageBlock", $Page->loadPagerTpl () );
			$this->view->assign ( 'Image_list', $Image_list );
		
		} else {
		}
		$this->render ( 'Content/attachment/albumload', null, true );
	}
	
	/**
	 * 管理缩略图
	 */
	public function showthumbAction() {
		$aid = $this->_request->get ( 'aid' );
		
		$this->render ( 'Content/attachment/showthumb', null, true );
	}
	
	/**
	 * 目录浏览
	 */
	public function albumdirAction() {
		$this->view->setScriptFile ( 'admin/jquery.imgpreview.js' );
		
		$dir = $this->_request->get ( 'dir' );
		$dir = str_replace ( '|', '/', $dir );
		
		$dir = isset ( $dir ) && trim ( $dir ) ? str_replace ( array (
				'..\\',
				'../',
				'./',
				'.\\' 
		), '', trim ( $dir ) ) : '';
		$filepath = UPLOAD_PATH . $dir;
		$list = glob ( $filepath . '/' . '*' );
		
		if (! empty ( $list )) {
			rsort ( $list );
		}
		
		$config = Zend_Registry::get ( 'config' );
		
		$local = str_replace ( array (
				UPLOAD_PATH,
				DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR 
		), array (
				'',
				'',
				DIRECTORY_SEPARATOR 
		), $filepath );
		$url = ($dir == '.' || $dir == '') ? $config->view_conf->siteUploadPath . '/' : $config->view_conf->siteUploadPath . '/' . str_replace ( '.', '', $dir ) . '/';
		$show_header = true;
		
		$file_list = '';
		
		if (is_array ( $list )) {
			$i = 1;
			foreach ( $list as $v ) {
				++ $i;
				$filename = basename ( $v );
				if (is_dir ( $v )) {
					// echo "**$dir**<br />";
					$dir = str_replace ( '/', '|', $dir );
					$file_list .= '<tr><td align="left"><img src="' . $config->view_conf->siteImgPath . '/folder-closed.gif" /> <a href="/content/attachment/albumdir/dir/' . (isset ( $dir ) && ! empty ( $dir ) ? ($dir) . '|' : '') . $filename . '/"><b>' . $filename . '</b></a></td></tr>';
				} else {
					$file_list .= '<tr id="img_' . $i . '"><td align="left" onclick="javascript:album_cancel(' . $i . ')"><img src="' . file_icon ( $filename, 'gif' ) . '" /> <a href="javascript:;" rel="' . $url . $filename . '" title="' . $filename . '">' . $filename . '</a></td></tr>';
				}
			}
		}
		
		$this->view->assign ( 'local', $local );
		$this->view->assign ( 'file_list', $file_list );
		$this->render ( 'Content/attachment/albumdir', null, true );
	}
}
?>